import route from "../routes.js";
import { Message, MessageBox } from "element-ui";
import axios from "axios";
import Vue from "vue";

// let base_url =
if (process.env.NODE_ENV === "development") {
  axios.defaults.baseURL = "/myapi"; // 开发环境
  console.log(axios.defaults.baseURL);
} else if (process.env.NODE_ENV === "production") {
  axios.defaults.baseURL = "/"; // 生产环境
}
// axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
// axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded';
axios.defaults.headers["content-type"] = "application/json;charset=utf8";

axios.create({
  headers: {
    "Content-Type": "application/json",
  },
});

/* 家政项目域名 */
const houseAxios = axios.create({
  baseURL: "https://zjsy.fcw-china.com/",
  // baseURL: 'https://house.fcw-china.com'
  // https://zjsy.fcw-china.com
});
Vue.prototype.$houseAxios = houseAxios;

/* 对公共账户认证/电子合同栏域名 */
const eleconAxios = axios.create({
  baseURL: "https://zjsy.fcw-china.com/elecon",
});
Vue.prototype.$eleconAxios = eleconAxios;

/* 电话招聘板块域名 */
const telAxios = axios.create({
  baseURL: "https://fcapp.fcw-china.com/app",
  // baseURL: "https://zjsy.fcw-china.com/app", 
  // baseURL: "http://192.168.11.149:8082/",
}) 
Vue.prototype.$telAxios = telAxios;
 
/* 灵活用工栏域名 */
const flexibleAxios = axios.create({
  // baseURL: 'https://house.fcw-china.com/app'
  baseURL: "https://fcapp.fcw-china.com/app",
  // baseURL: 'http://192.168.0.201:8082/'  
});

Vue.prototype.$flexibleAxios = flexibleAxios;

/* 发才网线下门店域名 */
const storesAxios = axios.create({
  // baseURL: 'https://house.fcw-china.com/app'
  baseURL: "https://fcapp.fcw-china.com/app",
  // baseURL: 'https://zjsy.fcw-china.com/app'
});

Vue.prototype.$storesAxios = storesAxios;

/* 工资单 */

const payrollAxios = axios.create({
  // baseURL: 'https://house.fcw-china.com/app'
  baseURL: "https://fcapp.fcw-china.com/app",
});
Vue.prototype.$payrollAxios = payrollAxios;

let ceshiTelAxios = (url, data) => {
  return telAxios({
    url: url,
    type: "post",
    data: data,
    responseType: "blob",
  }); 
};

Vue.prototype.$ceshiTelAxios = ceshiTelAxios;

/* 通用域名正式 fcapp*/
const ceshiAxios = axios.create({
  // baseURL: "https://house.fcw-china.com/app",
 
  baseURL: "https://fcapp.fcw-china.com/app",
  //  baseURL: " https://zjsy.fcw-china.com/app",
  // baseURL: "http://192.168.11.149:8082/",
  // baseURL: "http://192.168.11.149:8001/",
});
 

 

/* 微信小程序的域名正式house */
const ceshiAxiosWechat = axios.create({
  baseURL: "https://house.fcw-china.com/wechat",  
  // baseURL: "https://zjsy.fcw-china.com/wechat",
  // baseURL: "http://192.168.0.201:8082/wechat", 
  // baseURL: "http://192.168.0.145:8001/wechat",
  // baseURL: "http://192.168.0.20:8001/wechat",
  // baseURL: "http://192.168.11.149:8001/"
  // baseURL: "http://192.168.0.174:8001/wechat",
});

// baseURL: "http://192.168.0.114:8082/", 
//本地联调微信
const ceshiAxiosWechatceshi = axios.create({
 
  baseURL: "https://house.fcw-china.com/wechat",
  // baseURL: "https://zjsy.fcw-china.com/wechat",
  // baseURL: "http://192.168.0.145:8001/wechat",
  // baseURL: "http://192.168.11.149:8001/",
  // baseURL: "http://192.168.0.174:8001/wechat",
  // baseURL: "http://25suk6.natappfree.cc/wechat"
});

const ceshiAxios1 = axios.create({
  // baseURL: 'https://house.fcw-china.com/system', 
  baseURL: "https://fcapp.fcw-china.com/system",
  // baseURL: 'http://192.168.0.201:8081',
  // baseURL: 'https://zjsy.fcw-china.com/system'
});

const ceshiAxios2 = axios.create({
  // baseURL: 'https://house.fcw-china.com/system',//线上
  baseURL: "https://fcapp.fcw-china.com/system",
  // baseURL: 'http://115.159.24.62:8081',
  // baseURL: 'https://zjsy.fcw-china.com/app'
});

const ceshiAxios3 = axios.create({
  // baseURL: 'http://115.159.24.62:8081',//线上
  // baseURL: 'https://house.fcw-china.com/system',
  baseURL: "https://fcapp.fcw-china.com/system",
  // baseURL: 'http://115.159.24.62:8081',
  // baseURL: 'https://zjsy.fcw-china.com/app'
});

/* 测试 */
const ceshiAxiosys = axios.create({
  // baseURL: 'https://house.fcw-china.com/system'
  baseURL: "https://fcapp.fcw-china.com/system",
  // baseURL: 'https://zjsy.fcw-china.com/app'
});

let ceshiGetAxios = (url, data) => {
  return ceshiAxios({
    url: url,
    type: "get",
    params: data,
  });
};

let ceshiGetAxiosWechatExport = (url, data) => {
  return ceshiAxiosWechat({
    url: url,
    type: "get",
    params: data,
    responseType: "blob",
  });
};

 

let ceshiAxiosExportPost = (url, data) => {
  return ceshiAxios({
    url: url,
    method: "post",
    data: data,
    responseType: "blob",
  });
};

let ceshiAxiosExportPostWechat = (url, data) => {
  return ceshiAxiosWechat({
    url: url,
    method: "post",
    data: data,
    responseType: "blob",
  });
};

let ceshiAxiosWechatceshiGet = (url, data) => {
  return ceshiAxiosWechatceshi({
    url: url,
    type: "get",
    params: data,
  });
};
let ceshiGetAxiosWechat = (url, data) => {
  return ceshiAxiosWechat({
    url: url,
    type: "get",
    params: data,
  });
};
Vue.prototype.$ceshiGetAxios = ceshiGetAxios;
Vue.prototype.$ceshiGetAxiosWechat = ceshiGetAxiosWechat;
Vue.prototype.$ceshiGetAxiosWechatExport = ceshiGetAxiosWechatExport;
Vue.prototype.$ceshiAxiosExportPost = ceshiAxiosExportPost;
Vue.prototype.$ceshiAxiosExportPostWechat = ceshiAxiosExportPostWechat;

Vue.prototype.$ceshiAxios = ceshiAxios;
Vue.prototype.$ceshiAxiosWechat = ceshiAxiosWechat;
Vue.prototype.$ceshiAxiosWechatceshi = ceshiAxiosWechatceshi;
Vue.prototype.$ceshiAxiosWechatceshiGet = ceshiAxiosWechatceshiGet;
Vue.prototype.$ceshiAxios1 = ceshiAxios1;
Vue.prototype.$ceshiAxios2 = ceshiAxios2;
Vue.prototype.$ceshiAxios3 = ceshiAxios3;

let ceshiGetAxiosys = (url, data) => {
  return ceshiAxiosys({
    url: url,
    type: "get",
    params: data,
  });
};

let ceshiPostAxiosys = (url, data) => {
  return ceshiAxiosys({
    url: url,
    type: "post",
    data: data,
  });
};
Vue.prototype.$ceshiGetAxiosys = ceshiGetAxiosys;
Vue.prototype.$ceshiPostAxiosys = ceshiPostAxiosys;

// axios.$get = (url, data) => {
//   let d = data
//   // let postData = JSON.stringify({
//   //     requestSeq: "",
//   //     request: d
//   // });
//   // let postData = JSON.stringify(d)

//   return $ceshiAxios({
//     url: url,
//     method: 'get',
//     params: postData
//   })

// }

axios.$post = (url, data) => {
  let d = data;
  // let postData = JSON.stringify({
  //     requestSeq: "",
  //     request: d
  // });
  let postData = JSON.stringify(d);

  return axios({
    url: url,
    method: "post",
    data: postData,
  });
};

// axios.$get = (url, data) => {
//   let d = data
//   // let postData = JSON.stringify({
//   //     requestSeq: "",
//   //     request: d
//   // });
//   // let postData = JSON.stringify(d)

//   return axios({
//     url: url,
//     method: 'get',
//     params: postData
//   })

// }

// 添加响应拦截器
var a = 0;
axios.interceptors.response.use(
  function (response) {
    if (response.data.code === "1003") {
      Message.error("登陆有误，请重新登陆");
      route.push({
        path: "/login",
      });
      return Promise.reject("error");
    } else if (response.data.code === 123 || response.data.code === 128) {
      ++a;
      if (a == 1) {
        MessageBox.confirm("请重新登陆", "提示", {
          showClose: false,
          distinguishCancelAndClose: false,
          showCancelButton: false,
          closeOnClickModal: false,
          closeOnPressEscape: false,
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(() => {
          localStorage.clear();
          route.push({
            path: "/login",
          });
          a = 0;
          return Promise.reject(new Error("errTip"));
        });
      } else if (a > 1) {
        return Promise.reject(new Error("errTip"));
      }
    } else {
      return response;
    }
  },
  function (error) {
    // 对响应错误做点什么
    return Promise.reject(error);
  }
);

export default axios;
